package com.haiyuancloud.base.deal.ctr;

import com.alibaba.fastjson.JSON;
import com.haiyuancloud.base.deal.core.chain.enter.PageChainEnterFactory;
import com.haiyuancloud.base.deal.core.chain.parser.model.ChainResultModel;
import com.haiyuancloud.base.id.service.BaseIdService;
import com.haiyuancloud.common.core.utils.SessionDataUtils;
import com.haiyuancloud.db.core.model.DsDataModel;
import com.haiyuancloud.util.ObjectUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/run/ctr")
public class RunCtr extends BaseCtr {

    /***
     * 日志打印
     */
    private static Logger logger = LoggerFactory.getLogger(RunCtr.class);

    @Resource
    private PageChainEnterFactory pageChainEnterFactory;

    /**
     * empty
     */
    @ResponseBody
    @PostMapping("/empty")
    public Map<String, Object> empty() {
        Map<String, Object> submitData = this.getDefaultResultData();
        return submitData;
    }

    /**
     * 页面提交内容
     *
     * @param S_PAGE_ID  接口标志
     * @param S_PAGE_VER 接口版本
     * @param submitMap  接口参数
     * @return
     */
    @PostMapping("/submitJson/{S_PARTITION}/{S_PAGE_ID}/{S_PAGE_VER}")
    public Map<String, Object> submitJson(
            @PathVariable(value = "S_PARTITION") Integer S_PARTITION
            , @PathVariable(value = "S_PAGE_ID") String S_PAGE_ID
            , @PathVariable(value = "S_PAGE_VER") String S_PAGE_VER
            , @RequestBody(required=false) Map<String, Object> submitMap, HttpServletRequest request) {
        if(ObjectUtil.isNull(submitMap)){
            logger.warn(S_PAGE_ID+"提交参数为空." );
            submitMap =  new HashMap<>();
        }
        submitMap.put("S_PAGE_ID", S_PAGE_ID);
        submitMap.put("S_PAGE_VER", S_PAGE_VER);
        Map<String, Object> resultData = this.getDefaultResultData();
        resultData.put("S_PAGE_ID",submitMap.get("S_PAGE_ID"));
        resultData.put("S_PAGE_VER",submitMap.get("S_PAGE_VER"));
        logger.info("请求入参:" + JSON.toJSONString(submitMap));
        DsDataModel dsDataModel =  SessionDataUtils.getDsDataModel();
        dsDataModel.setOPEN_PARTITION(S_PARTITION);
        try {
            ChainResultModel chainResultModel = pageChainEnterFactory.chain(dsDataModel,S_PAGE_ID, S_PAGE_VER, submitMap);
            if(!"1".equals(chainResultModel.getResultCode())){
                Map<String,Object> data = new HashMap<>();
                data.put("success",false);
                data.put("resultCode", chainResultModel.getResultCode());
                resultData.put("resultMsg","操作失败");
                data.put("printMsg", chainResultModel.getErrorLog());
                resultData.putAll(data);
                resultData.put("data",data);
            }
            if(ObjectUtil.isNotNull(chainResultModel.getResultMsg())){
                resultData.put("message",chainResultModel.getResultMsg());
                resultData.put("resultMsg",chainResultModel.getResultMsg());
                resultData.put("printMsg",chainResultModel.getErrorLog());
            }
            if(ObjectUtil.isNotNull(chainResultModel.getData())) {
                resultData.putAll(chainResultModel.getData());
            }
        }catch (Exception e){
            Map<String,Object> data = new HashMap<>();
            data.put("success",false);
            data.put("resultMsg","操作异常.");
            data.put("resultCode", "2");
            data.put("printMsg", e.getMessage());
            resultData.putAll(data);
            resultData.put("data",data);
            e.printStackTrace();
        }
        return resultData;
    }
}
